package org.jeecg.modules.xindian.reconciliation.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysCategoryModel;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.xindian.reconciliation.controller.enums.ChannelSettlementConfigState;
import org.jeecg.modules.xindian.reconciliation.enums.DeleteFlagEnum;
import org.jeecg.modules.xindian.reconciliation.enums.SettlementType;
import org.springframework.format.annotation.DateTimeFormat;
import org.jeecgframework.poi.excel.annotation.Excel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * @Description: 保险结算配置
 * @Author: jeecg-boot
 * @Date: 2024-04-11
 * @Version: V1.0
 */
@Data
@TableName("insurance_settlement_config")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "insurance_settlement_config对象", description = "保险结算配置")
public class InsuranceSettlementConfig implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "id")
    private java.lang.String id;
    /**
     * 创建日期
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "创建日期")
    private java.util.Date createTime;
    /**
     * 更新日期
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "更新日期")
    private java.util.Date updateTime;
    /**
     * 保险公司
     */
    @Excel(name = "保险公司", width = 15, dicCode = "BXGS")
    @Dict(dicCode = "BXGS")
    @ApiModelProperty(value = "保险公司")
    private java.lang.String insurance;
    /**
     * 结算单位
     */
    @Excel(name = "结算单位", width = 15)
    @ApiModelProperty(value = "结算单位")
    private java.lang.String company;

    private String settlementConfig;

    /**
     * 结算规则
     */
    @Excel(name = "结算规则", width = 15, dicCode = "SC_JSGZ")
    @ApiModelProperty(value = "结算规则")
    @Dict(dicCode = "SC_JSGZ")
    private ChannelSettlementConfigState rule;
    /**
     * 是否删除
     */
    @Excel(name = "是否删除", width = 15, dicCode = "SC_SFSC")
    @ApiModelProperty(value = "是否删除")
    @Dict(dicCode = "SC_SFSC")
    private DeleteFlagEnum deleteFlag;
    /**
     * 是否删除
     */
    @Excel(name = "配置信息", width = 15)
    @ApiModelProperty(value = "配置信息")
    @TableField(exist = false)
    private List<SettlementConfig> configs;
    @ApiModelProperty(value = "结算区域列表")
    @TableField(exist = false)
    private List<String> areas;

    @ApiModelProperty(value = "结算平台名称")
    @TableField(exist = false)
    private List<String> platforms = new ArrayList<>();

    @ApiModelProperty(value = "阶梯经纪费比例配置")
    public List<SettlementConfig> buildConfig() {
        if (StringUtils.isNotBlank(this.settlementConfig)) {
            configs = JSON.parseArray(this.settlementConfig, SettlementConfig.class);
            return configs;
        }
        return null;
    }



    public void buildAreas() {
        ISysBaseAPI sysBaseAPI = SpringContextUtils.getBean(ISysBaseAPI.class);
        List<SysCategoryModel> sysCategoryModels = sysBaseAPI.queryAllSysCategory();
        Map<String, SysCategoryModel> sysCategoryModelMap =
                sysCategoryModels.stream().filter(sysCategoryModel -> sysCategoryModel.getCode().startsWith("B04")).collect(Collectors.toMap(SysCategoryModel::getUserCode, Function.identity()));
        if (CollectionUtils.isNotEmpty(configs)) {
            configs.stream().forEach(config -> {
                config.getPlatform().forEach(dt->{
                    SysCategoryModel model =   sysCategoryModelMap.get(dt);
                    Assert.notNull(model,"平台{}未配置区域信息",dt);
                    platforms.add(model.getName());
                });
            });
        }
    }

    @Data
    public static class SettlementConfig {
        /**结算平台*/
        @Excel(name = "平台", width = 15)
        @ApiModelProperty(value = "结算平台")
        private List<String> platform;
        @ApiModelProperty("结算区域")
        private java.lang.String area;
        @ApiModelProperty("结算类型")
        private SettlementType type;

        /**
         * 非阶梯经纪费比例
         */
        @Excel(name = "非阶梯经纪费比例", width = 15)
        @ApiModelProperty(value = "非阶梯经纪费比例")
        private java.math.BigDecimal fixRate;
        /**
         * 阶梯经纪费比例
         */
        @Excel(name = "阶梯经纪费比例", width = 15)
        @ApiModelProperty(value = "阶梯经纪费比例")
        private TrendsRateConfig trendsRateConfig;
    }

    @Data
    @ApiModel
    public static class TrendsRateConfig {
        @ApiModelProperty("最小值")
        private GTAndLT min;
        @ApiModelProperty("区间值")
        private List<BT> interval;
        @ApiModelProperty("最大值")
        private GTAndLT max;
    }

    @Data
    @ApiModel
    public static class GTAndLT {
        @ApiModelProperty("经纪费")
        private BigDecimal value;
        @ApiModelProperty("比例")
        private BigDecimal rate;
    }

    @Data
    @ApiModel
    public static class BT {
        @ApiModelProperty("大于经纪费")
        private BigDecimal lgt;
        @ApiModelProperty("小于经纪费")
        private BigDecimal lt;
        @ApiModelProperty("比例")
        private BigDecimal rate;
    }
}
